import type { SelectOption } from '../../../../../components/select/types'
import type { ThemeService } from '../../../../../types/constants'
import { ToolKit } from '../../../../../lib/utils/toolkit'
import { SUPPORTED_PLATFORMS } from '../../../../../types/constants'
import { ThemeType } from '../../../../../types/interfaces'
import { FiveOneCtoThemeService } from './blog/51_cto'
import { CSDNThemeService } from './blog/csdn'
import { JianShuThemeService } from './blog/jian_shu'
import { JueJinThemeService } from './blog/jue_jin'
import { ZhiHuThemeService } from './blog/zhi_hu'
import { BianThemeService } from './others/bian'
import { BilibiliThemeService } from './others/bilibili'
import { GiteeThemeService } from './others/gitee'
import { GithubThemeService } from './others/github'
import { GreasyForkThemeService } from './others/greasy_fork'
import { IconfontThemeService } from './others/iconfont'
import { BaiduThemeService } from './search/baidu'
import { BingThemeService } from './search/bing'
import { GoogleThemeService } from './search/google'
import { SoThemeService } from './search/so'
import { SogouThemeService } from './search/sogou'
import { ZhiHuQuestionThemeService } from './tiebar/zhi_hu_question/zhi_hu_question'
import { BilibiliVideoThemeService } from './video/bilibili_video'

/**
 * 构建主题选择器选项
 * @param customLabels 自定义标签
 */
export function buildThemeOptions(customLabels?: Partial<Record<ThemeType, string>>): SelectOption[] {
  // 默认标签转换函数
  const defaultLabelFormatter = (value: string): string => {
    // 处理下划线分隔的单词
    return value.split('_')
      .map(word => ToolKit.string.capitalizeFirstLetter(word))
      .join(' ')
  }

  // 默认标签文本
  const defaultLabels = Object.values(ThemeType).reduce((acc, theme) => {
    acc[theme] = defaultLabelFormatter(theme)
    return acc
  }, {} as Record<ThemeType, string>)

  // 合并自定义标签
  const labels = { ...defaultLabels, ...customLabels }

  return Object.values(ThemeType).map(theme => ({
    label: labels[theme],
    value: theme,
  }))
}

/**
 * 创建主题服务
 * @param platform 平台
 */
export function createThemeService(platform: string | undefined): ThemeService {
  switch (platform) {
    case SUPPORTED_PLATFORMS.CSDN:
      return CSDNThemeService.getInstance()
    case SUPPORTED_PLATFORMS.JIAN_SHU:
      return JianShuThemeService.getInstance()
    case SUPPORTED_PLATFORMS.JUE_JIN:
      return JueJinThemeService.getInstance()
    case SUPPORTED_PLATFORMS.ZHI_HU:
      return ZhiHuThemeService.getInstance()
    case SUPPORTED_PLATFORMS.FIVE_ONE_CTO:
      return FiveOneCtoThemeService.getInstance()
    case SUPPORTED_PLATFORMS.ZHI_HU_QUESTION:
      return ZhiHuQuestionThemeService.getInstance()
    case SUPPORTED_PLATFORMS.BILIBILI_VIDEO:
      return BilibiliVideoThemeService.getInstance()
    case SUPPORTED_PLATFORMS.BILIBILI:
      return BilibiliThemeService.getInstance()
    case SUPPORTED_PLATFORMS.GREASY_FORK:
      return GreasyForkThemeService.getInstance()
    case SUPPORTED_PLATFORMS.BAI_DU:
      return BaiduThemeService.getInstance()
    case SUPPORTED_PLATFORMS.GOOGLE:
      return GoogleThemeService.getInstance()
    case SUPPORTED_PLATFORMS.BING:
      return BingThemeService.getInstance()
    case SUPPORTED_PLATFORMS.SO_GOU:
      return SogouThemeService.getInstance()
    case SUPPORTED_PLATFORMS.SO:
      return SoThemeService.getInstance()
    case SUPPORTED_PLATFORMS.ICONFONT:
      return IconfontThemeService.getInstance()
    case SUPPORTED_PLATFORMS.BIAN:
      return BianThemeService.getInstance()
    case SUPPORTED_PLATFORMS.GITEE:
      return GiteeThemeService.getInstance()
    case SUPPORTED_PLATFORMS.GITHUB:
      return GithubThemeService.getInstance()
    default:
      return undefined
  }
}

export function getThemeOptions(): SelectOption[] {
  return buildThemeOptions({
    [ThemeType.LIGHT]: '晨曦微光',
    [ThemeType.DARK]: '暮色星河',
    [ThemeType.PINK]: '粉黛轻云',
  })
}
